﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.HVAC.LevelIndependentTag;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class DuctIndependentCommond : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
            base.FilterViewType.Add((ViewType)117);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			Transaction transaction = new Transaction(base.Doc, "添加共享参数");
			transaction.Start();
			MarkOperation markOperation = new MarkOperation();
			markOperation.AddAllShareParametes(base.UiApp);
			markOperation.SetDuctDiameterUnit(base.Doc);
			transaction.Commit();
			if (base.Doc.ActiveView is View3D && !((View3D)base.Doc.ActiveView).IsLocked)
			{
				MessageBox.Show("请锁定3D视图", "鸿业提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.frm = new FrmDuctIndependent();
			if (DuctIndependentCommond.hWndRevit == null)
			{
				DuctIndependentCommond.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			this.frm.Show(DuctIndependentCommond.hWndRevit);
			this.frm.commFont.LeadDistance = MarkPipeLevelUtility.GetTansferDistance(this.frm.commFont.LeadDistance, 30);
			this.frm.commFont.TubeDistance = MarkPipeLevelUtility.GetTansferDistance(this.frm.commFont.TubeDistance, 30);
			Result result;
			for (;;)
			{
				this.list = new List<Duct>();
				this.frm.IsChangeSel = false;
				try
				{
					if (this.frm.rboMultiple.Checked)
					{
						this.list = base.Sel.PickElementsByRectangle(new DuctSelectionFilter(), "请框选需要标注的管道").Cast<Duct>().ToList<Duct>();
						this.getFilterDuct(ref this.list);
					}
					else
					{
						Reference reference = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new DuctSelectionFilter(), "请选择风管标注点");
						this.GlobalPoint = reference.GlobalPoint;
						this.list.Add(base.Doc.GetElement(reference) as Duct);
					}
					new Transaction(base.Doc, "创建风管标注");
					transaction.Start();
					try
					{
						foreach (Duct ductTag in this.list)
						{
							new DuctIndependent(base.Doc, base.Sel, base.UiApp, base.UiDoc, this.frm, this.GlobalPoint, this.newgrpElements, this.fontHigtht, this.fontWidth).SetDuctTag(ductTag);
						}
						transaction.Commit();
					}
					catch (Exception ex)
					{
						if (transaction != null && (int)transaction.GetStatus() == 1)
						{
							transaction.Commit();
						}
						throw ex;
					}
					markOperation.SetActiveDocAllLoadedTags(base.Doc, false);
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					if (this.frm.IsChangeSel)
					{
						continue;
					}
					this.frm.Close();
					this.frm.Dispose();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex2)
				{
					this.frm.Close();
					MessageBox.Show(ex2.Message, "鸿业提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					result = Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			return result;
		}

		private void getFilterDuct(ref List<Duct> list)
		{
			list = (from s in list
			where s.GetCurveLength((BuiltInParameter)(-1004005)) * 1000.0 > this.frm.MarkSetting.TubeLength
			select s).ToList<Duct>();
		}

		public static YJKWindowHandle hWndRevit;

		private FrmDuctIndependent frm;

		private List<Duct> list;

		private XYZ GlobalPoint;

		private ElementSet newgrpElements = new ElementSet();

		private double fontHigtht = 1.0;

		private double fontWidth = 1.0;
	}
}
